package com.example.monitorapp.fragment


import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import com.example.baselib.constants.MyConstants
import com.example.baselib.dialog.MsgDialog
import com.example.baselib.fragment.BaseFragment

import com.example.monitorapp.R
import com.example.monitorapp.viewmodel.UserInfoViewModel
import kotlinx.android.synthetic.main.fragment_bind_tel.*

/**
 * A simple [Fragment] subclass.
 */
class BindTelFragment : BaseFragment<UserInfoViewModel>(){
    private var mCountDownTimer: CountDownTimer? =null

    override fun providerVMClass(): Class<UserInfoViewModel>? {
        return UserInfoViewModel::class.java
    }

    override fun contentLayout(): Int {
      return R.layout.fragment_bind_tel
    }

    override fun initView() {
        bind_tel_ed.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                isButtonEnable()
            }
        })
        bind_auth_ed.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                isButtonEnable()
            }
        })
        bind_send_txt.setOnClickListener { sendVef() }
        bind_sure_btn.setOnClickListener {
//            loadingDialog.show(activity!!.supportFragmentManager,"bindingTel")
            viewModel.bindingTel(bind_tel_ed.text.toString(),bind_auth_ed.text.toString()) }
    }

    override fun initData() {
        viewModel.getResult().observe(this, Observer {
            MsgDialog(it.msg).setCallbackListener(object :MsgDialog.onDismissCallback{
                override fun dismiss() {
                    MyConstants.userData.tel=bind_tel_ed.text.toString()
                    activity?.onBackPressed()
                }
            }).show(activity!!.supportFragmentManager,"getResult")
        })
    }


    /**
     * 控制button
     */
    private fun isButtonEnable() {
        bind_sure_btn.isEnabled =
            !(bind_tel_ed.text.isNullOrEmpty()||bind_auth_ed.text.isNullOrEmpty())
    }
    /**
     * @Description: [发送验证码]
     * @Param:
     * @Return:
     */
    private fun sendVef() {
        if (bind_tel_ed.text.isNullOrEmpty()) {
            Toast.makeText(activity, "请填写手机号", Toast.LENGTH_SHORT).show()
        } else {
            countDown()
            bind_send_txt.isEnabled = false
            viewModel.getVerificationCode(bind_tel_ed.text.toString())
        }
    }
    /**
     * 60秒倒计时
     */
    private fun countDown(){
        var count = 0
        mCountDownTimer= object : CountDownTimer((60 * 1000).toLong(), (1 * 1000).toLong()) {
            override fun onTick(millisUntilFinished: Long) {

                bind_send_txt.text = ((60 - count).toString()) + "s"
                count++
            }

            override fun onFinish() {
                mCountDownTimer?.cancel()
                count = 1
                bind_send_txt.isEnabled = true
                bind_send_txt.text = "获取验证码"
            }
        }
        mCountDownTimer?.start()

    }

    override fun onStop() {
        super.onStop()
        mCountDownTimer?.cancel()
    }
}
